package org.sunsys.solr.solrj;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

public class SolrDAO {

	private static Logger logger = Logger.getLogger(SolrDAO.class.getName());
	
	private SolrServer solrServer = null;

	public SolrDAO() {
	}

	public void setSolrServer(SolrServer solrServer) {
		this.solrServer = solrServer;
	}

	public SolrServer getSolrServer() {
		return solrServer;
	}

	public SolrPingResponse ping() throws SolrServerException, IOException {
		SolrPingResponse pingResponse = solrServer.ping();
		return pingResponse;
	}

	public UpdateResponse deleteDocument(Map<String, String> map, String []compositePrimaryKeyFiledsNames) throws SolrServerException, IOException {
		//TODO yet to test this method
		StringBuilder query = new StringBuilder("");
		int length = compositePrimaryKeyFiledsNames.length -1;
		int i=0;
		for(; i<length; i++) {
			query.append(compositePrimaryKeyFiledsNames[i]).append(":").append(map.get(compositePrimaryKeyFiledsNames[i]));
			query.append(" ");
		}
		
		if(i <= length ){
			query.append(compositePrimaryKeyFiledsNames[i]).append(":").append(map.get(compositePrimaryKeyFiledsNames[i]));
		}
		
		if(logger.isDebugEnabled()) {
			logger.debug("delete query :"+query);
		}
		
		return solrServer.deleteByQuery(query.toString());
	}
	
	public UpdateResponse uploadData(Map<String, String> map) throws SolrServerException, IOException {

		SolrInputDocument solrInputDocument = null;
		solrInputDocument = new SolrInputDocument();

		for (Entry<String, String> entity : map.entrySet()) {
			String columnName = entity.getKey();
			String value = entity.getValue();
			/* add field to Solr Input Document */
			solrInputDocument.addField(columnName, value);
		}

		UpdateResponse updateResponse = solrServer.add(solrInputDocument);
		
		return updateResponse;
	}
	
	public UpdateResponse deleteAndUpdate(Map<String, String> map, String []compositePrimaryKeyFiledsNames) throws SolrServerException, IOException {
		//TODO yet to test this method
		UpdateResponse deleteDocumentResponse= deleteDocument(map, compositePrimaryKeyFiledsNames);
		logger.debug("Delete Document- Composite key"+compositePrimaryKeyFiledsNames+" Status:"+deleteDocumentResponse.getStatus());
		return uploadData(map);
	}
	
	public UpdateResponse commitChanges() throws SolrServerException, IOException {
		return solrServer.commit();
	}
	
	public static void main(String[] args) throws Exception {

		SolrDAO solrOperations = new SolrDAO();

		String url = "http://192.168.17.205:8983/solr/question_answers/";
		SolrServer solrServer = null;
		solrServer = new LBHttpSolrServer(url);
		/* set XML Response Parser */
		((LBHttpSolrServer) solrServer).setParser(new XMLResponseParser());
		
		/* set Solr server */
		solrOperations.setSolrServer(solrServer);
		/* ping to server */
		SolrPingResponse pingResponse = solrOperations.ping();
		System.out.println("Ping QTime:" + pingResponse.getQTime());

//		Map<String, String> columnValuesMap = new HashMap<String, String>();
//		columnValuesMap.put("questionId", "10004");
//		columnValuesMap.put("question", "Explain about Thread");
//		/* update solr */
//		UpdateResponse updateResponse = solrOperations.uploadData(columnValuesMap, true);
//		System.out.println("Update Document QTime:" + updateResponse.getQTime());

		System.out.println("Thank you");
	}
}
